<?php
/**
 * @desc 产品大类字段管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_generalfield extends ctrl_base {
    private static $dirName = 'generalfield';
    
    /**
     * @desc 产品大分类列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('generalfield_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$class_name = isset($_POST['class_name']) ? trim($_POST['class_name']) : '';
    	//搜索条件组合
		$condition = array();
    	if(!empty($class_name)) {
    		$condition['class_name'] = $class_name;
    	}
		//订单获取
    	$produce_list = model_generalfield :: get_generalfield_list($nextpage,$pagesize,$condition);
    	$total = array_pop($produce_list);
		//模板赋值
    	self::smarty() -> assign('produce_list', $produce_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('class_name', $class_name);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/generalfield_list.html');    	
    }
	//公用字段维护
	public static function get_common_field(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('generalfield_do'));

		$class_id = intval($_GET['class_id']);
		$formula_table = model_generalfield::get_general_field($class_id);
		//构建分类树形
		$formula_table['price_list'] = self::build_tree($formula_table['price_list'], 0);
		//统计每级分类个数
		$formula_table['price_list'] = self::count_array_childs($formula_table['price_list']);
		$formula_table = self::create_table_data($formula_table);
		$table_data = self::create_table_contain($formula_table);
		self::smarty() -> assign('formula_table', $formula_table);
		self::smarty() -> assign('table_data', $table_data);
		self::smarty() -> assign('class_id', $class_id);
		self::smarty() -> display(self::$dirName . '/generalfield_table.html');
	}
	//保存表格数据
	public static function table_save(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('generalfield_do'));
		$data = self::get_request_data();
		$parent_id = intval($data['parent_id']);
		if(empty($data['cell_price']) || empty($parent_id)){
			self::ajax_ouput(300, '操作失败,非法操作');
		}
		$formula_table = model_generalfield::get_general_field($parent_id);
		$dataarr = array();
		$i = 0;
		foreach($data['cell_price'] as $key => $value){
			$dataarr[$i][':produce_id'] = $formula_table['modify'][$key];
			$dataarr[$i][':attr_id'] = $key;
			$dataarr[$i][':attr_value'] = json_encode($value);
			$i++;
		}
		$result = model_formula::insert_general_table_data($dataarr);
		if(!empty($result)){
			self::ajax_ouput(200, '操作成功','generalfield_do');
		}else{
			self::ajax_ouput(300, '操作失败');
		} 
	}
}
